<?php
//注册验证
   class RegisterMatchController extends Yaf\Controller_Abstract{
       public function init(){
           Yaf\Dispatcher::getInstance()->disableView();
       }
       public function checkedAction(){
           $name = $_POST['username'];
           $pass = $_POST['password'];
           $sex = $_POST['sex'];
           $tel = $_POST['tel'];
           $email = $_POST['email'];
           $introduce = $_POST['introduce'];
//         验证用户姓名
//           Post::where('views','>',0)->select('id','title','content')->get();
           $username = UsersModel::where('username','=',$name)->get();
           $checkemail = UsersModel::where('email','=',$email)->get();
           $checktel = UsersModel::where('tel','=',$tel)->get();

           if (count($username)>0){
//               用户名被占用
               $msg = "用户名已经被占用了，换个名字吧";
               $flag=false;
           }else
//         验证邮箱
           if (count($checkemail)>0){
//               邮箱已被注册
               $msg = "邮箱名被占用，换个邮箱吧";
               $flag=false;
           }else
//           验证手机号
           if (count($checktel)>0){
//               手机号已被注册
               $msg = "手机号已被注册，换个手机号吧";
               $flag=false;
           }else{
//         拼接salt字段然后进行md5加密
           $salt = rand(10000,99999);
           $newPass = md5(''.$pass.$salt);
           $type = '1';
           $status = '0';

//           激活码
           $to = rand(100,999);
           $token = md5(''.$newPass.$to);

//         对个人简介进行敏感词过滤
//           定义简单的敏感词数组
           $list = ['我去','我丢','我靠','我草','我擦','傻逼','傻屌','煞笔','傻狗'];
//           替换后的数据
           $newIntro = $this->sensitive($list,$introduce);
//         进行信息存储（type，status）
            $users = new UsersModel();
            $users->username = $name;
            $users->password = $newPass;
            $users->salt = $salt;
            $users->sex = $sex;
            $users->tel = $tel;
            $users->email = $email;
            $users->introduce = $newIntro;
            $users->type = $type;
            $users->status = $status;
            $users->token = $token;
            $users->save();
//         发送邮箱进行验证（激活）
            $content =  "亲爱的".$name."：<br/>感谢您在我站注册了新帐号。<br/>请点击链接激活您的帐号。<br/> 
    <a href='yaf.csf.com/RegistSuccess/successRegis?verify=".$token."' target= 
'_blank'>yaf.csf.com/RegistSuccess/successRegis?verify=".$token."</a><br/> 
    如果以上链接无法点击，请将它复制到你的浏览器地址栏中进入访问。";
               $sendMails = new MailsModel();
            $results = $sendMails->sendMail($email,'注册成功',$content);
            if ($results == '发送成功'){
                $msg = "注册成功，请前往邮箱进行账户激活！";
                $flag=true;
            }
           }

           $arr=array("msg"=>$msg,"flag"=>$flag);
           echo json_encode($arr);
       }

//       简单的敏感词过滤，后期可以试着使用数据结构中的前缀树对敏感词进行过滤，空间换时间
       function sensitive($list, $string){
           $count = 0; //违规词的个数
           $sensitiveWord = ''; //违规词
           $stringAfter = $string; //替换后的内容
           $pattern = "/".implode("|",$list)."/i"; //定义正则表达式
           if(preg_match_all($pattern, $string, $matches)){ //匹配到了结果
               $patternList = $matches[0]; //匹配到的数组
               $count = count($patternList);
               $sensitiveWord = implode(',', $patternList); //敏感词数组转字符串
               $replaceArray = array_combine($patternList,array_fill(0,count($patternList),'*')); //把匹配到的数组进行合并，替换使用
               $stringAfter = strtr($string, $replaceArray); //结果替换
           }
           return $stringAfter;
       }
   }